import { _decorator, Component, instantiate, Node, UITransform, Vec2 } from 'cc';
import DataManager from '../../Global/DataManager';
import { EntityTypeEnum, IActor, InputTypeEnum, toFixed } from '../../Common';
import { EntityManager } from '../../Base/EntityManager';
import { EntityStateEnum, EventEnum } from '../../Enum';
import { WeaponStateMachine } from './WeaponStateMachine';
import EventManager from '../../Global/EventManager';
const { ccclass, property } = _decorator;
/**
 * 武器类
 */
@ccclass('WeaponManager')
export class WeaponManager extends EntityManager {

    owner:number
    type:EntityTypeEnum

    private body:Node
    private anchor:Node
    private point:Node
    
    init({id,weaponType}:{id:number;weaponType:EntityTypeEnum}){
        this.owner=id
        this.type = weaponType
        //设置层级最低
        this.node.setSiblingIndex(0);
        this.body = this.node.getChildByName("Body");
        this.anchor = this.body.getChildByName("Anchor");
        this.point = this.anchor.getChildByName("Point");
        
        this.fsm=this.body.addComponent(WeaponStateMachine)
        this.fsm.init(weaponType)
        //设置默认状态，并且执行其动画
        this.state=EntityStateEnum.Idle

        //监听ShootManager的点击事件
        EventManager.Instance.on(EventEnum.BulletBron,this.HandleBulletBron,this)
        EventManager.Instance.on(EventEnum.WeaponShoot,this.HandleWeaponShoot,this)

    }
    onDestroy(){
        EventManager.Instance.off(EventEnum.WeaponShoot,this.HandleWeaponShoot,this)
        EventManager.Instance.off(EventEnum.BulletBron,this.HandleBulletBron,this)
    }
    /**
     * 处理子弹生成枪变成攻击状态
     */
    HandleBulletBron(owner:number){
       if(this.owner!==owner){
        return
       }     
       this.state=EntityStateEnum.Attack
    //    console.log("玩家：",owner,"武器状态：",this.state);
       
    }

    /**
     * 封装input
     */
    HandleWeaponShoot(){
        if(this.owner!==DataManager.Instance.myPlayerId){
            return
        }
        //获取point节点的世界坐标
        const pointWorldPos = this.point.getWorldPosition()
        //子弹是生成在舞台节点的，所以要把子弹的坐标转换到舞台节点下 convertToNodeSpaceAR(pointWorldPos)是吧入参转成当前节点的坐标
        const pointStagePos = DataManager.Instance.stage.getComponent(UITransform).convertToNodeSpaceAR(pointWorldPos)
        const anchorWorldPos = this.anchor.getWorldPosition()
        //获取方向向量，并且归一化
        const direction = new Vec2(pointWorldPos.x-anchorWorldPos.x,pointWorldPos.y-anchorWorldPos.y).normalize()
        //发送input
        // DataManager.Instance.applyInput({
            // type:InputTypeEnum.WeaponShoot,
            // owner:this.owner,
            // position:{
            //     x:pointStagePos.x,
            //     y:pointStagePos.y,
            // },
            // direction:{
            //     x:direction.x,
            //     y:direction.y,
            // },
            
        // })
        
         //使用clientSync事件（battlemanager监听事件）来向服务端发送input输入值
         EventManager.Instance.emit(EventEnum.ClientSync,{
            type:InputTypeEnum.WeaponShoot,
            owner:this.owner,
            position:{
                x:toFixed(pointStagePos.x),
                y:toFixed(pointStagePos.y),
            },
            direction:{
                x:toFixed(direction.x),
                y:toFixed(direction.y),
            },
        })
        // console.log(DataManager.Instance.state.bullets)
    }
}

